import 'package:FineArt/core/http/base_dio.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:retrofit/retrofit.dart';
import 'package:dio/dio.dart';
import '../../constants.dart';

part 'resource_api.g.dart';

@RestApi(baseUrl: kBaseUrl)
abstract class ResourceApi {
  factory ResourceApi({Dio dio, String baseUrl}) {
    dio ??= BaseDio.getInstance().getDio();
    return _ResourceApi(dio, baseUrl: baseUrl);
  }

  @GET("/api/v1/asset/{id}/{type}") //打开指定资源的详细信息（包括高清，图书图片序列等相关信息）
  Future<ResourceDetailResponse> resourceDetail(@Path("id") int resourceId,
      @Path("type") int type);

  @POST("/api/v1/favorite") //收藏
  Future<FavoriteResponse> favorite(@Body() FavoriteReqBody body);

  @DELETE("/api/v1/favorite/{id}") //收藏
  Future<FavoriteResponse> deleteFavorite(@Path("id") int favoriteId);

  @DELETE("/api/v1/share/{id}") //删除分享
  Future<ShareResponse> deleteShare(@Path("id") int shareId);

  @POST("/api/v1/share") //分享
  Future<ShareResponse> share(@Body() ShareBody body);

  @GET("/api/v1/favorite") //拉取收藏
  Future<FavoriteListResponse> favoriteList(@Query("pg") int page,
      @Query("size") int size);

  @GET("/api/v1/share/withme") //拉取分享
  Future<Share2MeResponse> shareList(@Query("pg") int page,
      @Query("size") int size);

  @GET("/api/v1/share/mine") //拉取分享
  Future<ShareByMeResponse> myShareList(@Query("pg") int page,
      @Query("size") int size);
}

////////////////////////////
@JsonSerializable()
class ResourceDetailResponse {
  String requestId;
  int code;
  String msg;
  ResourceDetail data;

  ResourceDetailResponse({this.code, this.msg, this.requestId, this.data});

  factory ResourceDetailResponse.fromJson(Map<String, dynamic> json) =>
      _$ResourceDetailResponseFromJson(json);
}

class ResourceDetail {
  String title; //  课件 title
  int type; //  资源类型
  Object info; ////  基于资源类型的资源结构描述,不同的type这块内容可能不一样，下面会单独描述

  ResourceDetail.fromJson(Map<String, dynamic> json) {
    title = json['title'] as String;
    type = json['type'] as int;
    if (type == 1 || type == 5001) {
      info = json['info'] == null
          ? null
          : ResourceInfo1.fromJson(json['info'] as Map<String, dynamic>);
    } else if (type == 2 || type == 5002) {
      info = json['info'] == null
          ? null
          : ResourceInfoVideo.fromJson(json['info'] as Map<String, dynamic>);
    } else if (type == 0 || type == 5000) {
      info = json['info'] == null
          ? null
          : ResourceInfo0.fromJson(json['info'] as Map<String, dynamic>);
    }
  }

  Map<String, dynamic> toJson() => {};
}

// type=0  图书
@JsonSerializable()
class ResourceInfo0 {
  String ext;
  String url;
  String sdurl;
  String hdurl;
  String author;
  String publisher;
  String publishat;
  String cover;
  int totalpages;
  List<int> curpages;

  ResourceInfo0({this.ext,
    this.url,
    this.sdurl,
    this.hdurl,
    this.author,
    this.publishat,
    this.publisher,
    this.cover,
    this.totalpages});

  factory ResourceInfo0.fromJson(Map<String, dynamic> json) =>
      _$ResourceInfo0FromJson(json);
}

// type=1      基于单独图像的资源（包括书籍，照片，作品等）
@JsonSerializable()
class ResourceInfo1 {
  String ext;
  String url;
  String sdurl;
  String hdurl;

  ResourceInfo1({this.ext, this.url, this.sdurl, this.hdurl});

  factory ResourceInfo1.fromJson(Map<String, dynamic> json) =>
      _$ResourceInfo1FromJson(json);
}

//type=2     教学
@JsonSerializable()
class ResourceInfo2 {
  String picture; // 实物照片地址
  String techart; // 范画地址
  ResourceDecomposite decomposite; //分解教学资源
  String video; //视频地址

  ResourceInfo2({this.picture, this.techart, this.decomposite, this.video});

  factory ResourceInfo2.fromJson(Map<String, dynamic> json) =>
      _$ResourceInfo2FromJson(json);
}

@JsonSerializable()
class ResourceInfoVideo {
  String thumburl;
  String ext;
  String url;
  String author; //"作者"
  String publisher; // "发布者"
  String publishat; //2021-09-21 21:00:00"
  String seriesname; // "系列名"
  String albumname; //“集名”
  String path;

  ResourceInfoVideo({this.thumburl,
    this.ext,
    this.url,
    this.author,
    this.publisher,
    this.publishat,
    this.seriesname,
    this.albumname});

  factory ResourceInfoVideo.fromJson(Map<String, dynamic> json) =>
      _$ResourceInfoVideoFromJson(json);
}

//分解教学的数据结构
@JsonSerializable()
class ResourceDecomposite {
  String image; //分解绘图序列图像的根地址
  String ext; // 图片类型
  int pages; //这个资源下总共有多少张图片

  ResourceDecomposite({this.image, this.ext, this.pages});

  factory ResourceDecomposite.fromJson(Map<String, dynamic> json) =>
      _$ResourceDecompositeFromJson(json);
}

//////////////////////
@JsonSerializable()
class FavoriteReqBody {
  int resid;
  int type;
  String url;
  String title;
  String path;
  int subitem;

  FavoriteReqBody(
      {this.resid, this.type, this.url, this.title, this.path, this.subitem});

  Map<String, dynamic> toJson() => _$FavoriteReqBodyToJson(this);
}

//////////////////////
@JsonSerializable()
class FavoriteResponse {
  int code;
  String msg;
  String requestId;

  FavoriteResponse({this.code, this.msg, this.requestId});

  factory FavoriteResponse.fromJson(Map<String, dynamic> json) =>
      _$FavoriteResponseFromJson(json);
}

/////////////////
@JsonSerializable()
class ShareBody {
  int resid;
  int type;
  String url;
  String title;
  List<String> uids;
  int subitem;

  ShareBody(
      {this.resid, this.type, this.url, this.title, this.uids, this.subitem});

  Map<String, dynamic> toJson() => _$ShareBodyToJson(this);
}

//////////////////////
@JsonSerializable()
class ShareResponse {
  int code;
  String requestId;
  String msg;

  ShareResponse({this.code, this.requestId, this.msg});

  factory ShareResponse.fromJson(Map<String, dynamic> json) =>
      _$ShareResponseFromJson(json);
}

/////////////////////////
@JsonSerializable()
class Share2MeResponse {
  int code;
  String msg;
  String requestId;
  Share2MeData data;

  Share2MeResponse({this.code, this.msg, this.requestId, this.data});

  factory Share2MeResponse.fromJson(Map<String, dynamic> json) =>
      _$Share2MeResponseFromJson(json);
}

@JsonSerializable()
class Share2MeData {
  int count;
  int pageIndex;
  int pageSize;
  List<Share2Me> list;

  Share2MeData({this.count, this.pageIndex, this.pageSize, this.list});

  factory Share2MeData.fromJson(Map<String, dynamic> json) =>
      _$Share2MeDataFromJson(json);
}

@JsonSerializable()
class Share2Me {
  int id;
  String FromUid;
  String label;
  int restype;
  int resid;
  int sort;
  int path;
  int subitem;
  String title;
  String url;
  String sdurl;
  String nickname;
  String updatedat;

  @JsonKey(defaultValue: false)
  bool isChecked;

  Share2Me({this.id,
    this.FromUid,
    this.label,
    this.restype,
    this.resid,
    this.sort,
    this.path,
    this.title,
    this.subitem,
    this.url,
    this.sdurl,
    this.nickname,
    this.updatedat});

  factory Share2Me.fromJson(Map<String, dynamic> json) =>
      _$Share2MeFromJson(json);
}

//////////////////////////////
/////////////////////////
@JsonSerializable()
class ShareByMeResponse {
  int code;
  String msg;
  String requestId;
  ShareByMeData data;

  ShareByMeResponse({this.code, this.msg, this.requestId, this.data});

  factory ShareByMeResponse.fromJson(Map<String, dynamic> json) =>
      _$ShareByMeResponseFromJson(json);
}

@JsonSerializable()
class ShareByMeData {
  int count;
  int pageIndex;
  int pageSize;
  List<ShareByMe> list;

  ShareByMeData({this.count, this.pageIndex, this.pageSize, this.list});

  factory ShareByMeData.fromJson(Map<String, dynamic> json) =>
      _$ShareByMeDataFromJson(json);
}

@JsonSerializable()
class ShareByMe {
  int id;
  String FromUid;
  String label;
  int restype;
  int resid;
  int sort;
  int path;
  String title;
  String url;
  String sdurl;
  String nickname;
  String updatedat;
  int subitem;

  @JsonKey(defaultValue: false)
  bool isChecked;

  ShareByMe({this.id,
    this.FromUid,
    this.label,
    this.restype,
    this.resid,
    this.sort,
    this.path,
    this.title,
    this.url,
    this.sdurl,
    this.nickname,
    this.subitem,
    this.updatedat});

  factory ShareByMe.fromJson(Map<String, dynamic> json) =>
      _$ShareByMeFromJson(json);
}
//////////////////////////////

/////////////////
//////////////////////
@JsonSerializable()
class FavoriteListResponse {
  int code;
  String requestId;
  String msg;
  FavoriteListData data;

  FavoriteListResponse({this.code, this.requestId, this.msg, this.data});

  factory FavoriteListResponse.fromJson(Map<String, dynamic> json) =>
      _$FavoriteListResponseFromJson(json);
}

//////////////////////
@JsonSerializable()
class FavoriteListData {
  int count;
  int pageIndex;
  int pageSize;
  List<Favorite> list;

  FavoriteListData({this.count, this.pageIndex, this.pageSize, this.list});

  factory FavoriteListData.fromJson(Map<String, dynamic> json) =>
      _$FavoriteListDataFromJson(json);
}

//////////////////
@JsonSerializable()
class Favorite {
  int id;
  String uid;
  int restype;
  int resid;
  int sort;
  int partenerid;
  String title;
  String path;
  String url;
  int subitem;
  @JsonKey(defaultValue: false)
  bool isChecked;
  @JsonKey(defaultValue: "2021-05-29")
  String updatedat;

  Favorite({this.id,
    this.uid,
    this.restype,
    this.resid,
    this.sort,
    this.partenerid,
    this.title,
    this.path,
    this.url,
    this.subitem,
    this.updatedat});

  factory Favorite.fromJson(Map<String, dynamic> json) =>
      _$FavoriteFromJson(json);

  Map<String, dynamic> toJson() => _$FavoriteToJson(this);
}

/////////////////
//////////////////////
